/* Copyright (C) 2008 Lorenzo Braghetto
 * 
 * This file is part of BatteryDiff Widget.
 * 
 *   BatteryDiff Widget is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *   
 *  BatteryDiff Widget is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
*/

package com.mono.diffwidget;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.widget.RemoteViews;

public class BatteryDiffService extends Service {

	public static int currentBatteryLevel = 0;
	public static final String PREFS_NAME = "DiffWidgetPrefs";
	public static final String PREFS_NAME2 = "TimePref";
	private static final int HELLO_ID = 1;
	public static Notification notification;
	public static NotificationManager mNotificationManager;
	CharSequence contentText;
	String text;
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// power_changed
		BroadcastReceiver mBI = new BatteryDiffStatusC();
		IntentFilter mIntentFilter = new IntentFilter();
		mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(mBI, mIntentFilter);
		// power_disconnected
		BroadcastReceiver mBID = new BatteryDiffStatusD();
		IntentFilter mIntentFilterD = new IntentFilter();
		mIntentFilterD.addAction(Intent.ACTION_POWER_DISCONNECTED);
		mIntentFilterD.addAction(Intent.ACTION_POWER_CONNECTED);
		registerReceiver(mBID, mIntentFilterD);
		
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

		String minuti = settings.getString("minuti", "");
		String ore = settings.getString("ore", "");
		SharedPreferences settings2 = getSharedPreferences(PREFS_NAME2, 0);
		boolean incarica = settings2.getBoolean("incarica", false);
		boolean checked = settings2.getBoolean("notification", false);

		if (checked) {
			if (incarica) {
				text = "Charging";
			} else if (minuti == "" && ore == "") {
				text = "Wait";
			} else {
				text = ore + ":" + minuti;
			}

			Context context = getApplicationContext();
			CharSequence contentTitle = "BatteryDiff:";
			Intent notificationIntent = new Intent(this, About.class);
			PendingIntent contentIntent = PendingIntent.getActivity(this,
					0, notificationIntent, 0);
			String ns = Context.NOTIFICATION_SERVICE;
			mNotificationManager = (NotificationManager) getSystemService(ns);
			int icon = R.drawable.icon;
			CharSequence tickerText = "BatteryDiff Started";
			long when = System.currentTimeMillis();
			notification = new Notification(icon, tickerText, when);
			notification.flags |= Notification.FLAG_ONGOING_EVENT;
			notification.flags |= Notification.FLAG_NO_CLEAR;

			contentText = String.valueOf(text);

			notification.setLatestEventInfo(context, contentTitle,
					contentText, contentIntent);

			mNotificationManager.notify(HELLO_ID, notification);
		}
		BatteryDiffWidget.text(this, currentBatteryLevel);
		
		return 0;
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public static class BatteryDiffStatusC extends BroadcastReceiver {

		public void onReceive(Context context, Intent intent) {
						
			int carica = intent.getIntExtra("status", 0);
			currentBatteryLevel = intent.getIntExtra("level", 0);
			
			SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
			SharedPreferences settings2 = context.getSharedPreferences(PREFS_NAME2, 0);
			Editor settingsE = settings2.edit();
			float temperature = intent.getIntExtra("temperature", 0)/10;
			settingsE.putFloat("temperature", temperature);
			settingsE.commit();
			
			String battcarica = "";
			if (carica == 2)// On Charge
			{

				boolean batt = settings2.getBoolean("batt", false);
				if (batt) {
					battcarica = "\n" + String.valueOf(currentBatteryLevel)
							+ "%";
				}
				RemoteViews views = BatteryDiffWidget.buildView(context,
						"Charging" + battcarica);
				ComponentName widget = new ComponentName(context,
						BatteryDiffWidget.class);
				AppWidgetManager manager = AppWidgetManager
						.getInstance(context);
				manager.updateAppWidget(widget, views);

				SharedPreferences.Editor editor2 = settings2.edit();
				editor2.putBoolean("incarica", true);
				editor2.commit();
			} else// On Discharge
			{
				String text = settings.getString("text", "Wait");
				boolean batt = settings2.getBoolean("batt", false);
				if (batt) {
					battcarica = "\n" + String.valueOf(currentBatteryLevel)
							+ "%";
				}
				RemoteViews views = BatteryDiffWidget.buildView(context, text
						+ battcarica);
				ComponentName widget = new ComponentName(context,
						BatteryDiffWidget.class);
				AppWidgetManager manager = AppWidgetManager
						.getInstance(context);
				manager.updateAppWidget(widget, views);
			}
		}

	}

	public static class BatteryDiffStatusD extends BroadcastReceiver {

		public void onReceive(Context context, Intent intent) {
			
			final String action = intent.getAction();
			SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
			SharedPreferences settings2 = context.getSharedPreferences(PREFS_NAME2, 0);

			if (intent.ACTION_POWER_CONNECTED.equals(action)) {
				SharedPreferences.Editor editor = settings.edit();
				editor.clear();
				editor.commit();
				SharedPreferences.Editor editor2 = settings2.edit();
				editor2.putBoolean("incarica", true);
				editor2.commit();
			}
			if (intent.ACTION_POWER_DISCONNECTED.equals(action)) {
				String battcarica = "";
				boolean batt = settings2.getBoolean("batt", false);
				if (batt) {
					battcarica = "\n" + String.valueOf(currentBatteryLevel)
							+ "%";
				}
				RemoteViews views = BatteryDiffWidget.buildView(context, "Wait"
						+ battcarica);
				ComponentName widget = new ComponentName(context,
						BatteryDiffWidget.class);
				AppWidgetManager manager = AppWidgetManager
						.getInstance(context);
				manager.updateAppWidget(widget, views);

				SharedPreferences.Editor editor = settings.edit();
				editor.clear();
				editor.commit();
				SharedPreferences.Editor editor2 = settings2.edit();
				editor2.putBoolean("incarica", false);
				editor2.commit();
			}
		}

	}

}
